//############################################################################
//## WiiDS Emulator (DeSmuME Port)
//##--------------------------------------------------------------------------
//## wiids.cpp
//## - Controls program flow. All things start and end here.
//############################################################################

#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogcsys.h>
#include <unistd.h>
#include <wiiuse/wpad.h>
#include <sys/iosupport.h>
#include <di/di.h>

#include "includes/FreeTypeGX.h"

//#include "ndssupport.h"
#include "preferences.h"
#include "audio.h"
#include "filebrowser.h"
#include "fileop.h"
#include "networkop.h"
#include "menu.h"
#include "input.h"
#include "video.h"
#include "wiids.h"
#include "wiiusbsupport.h"

extern "C" {
	extern void __exception_setreload(int t);
}

int ScreenshotRequested = 0;
int ConfigRequested = 0;
int ShutdownRequested = 0;
int ResetRequested = 0;
int ExitRequested = 0;
char appPath[1024] = { 0 };
char loadedFile[1024] = { 0 };

//----------------------------------------------------------------------------
// WIIDS - ExitCleanup
//----------------------------------------------------------------------------
static void ExitCleanup()
{
//	NDS_DeInit(); // Shutdown NDS
	ShutdownAudio(); // Shutdown Audio
	StopGX(); // Shutdown GFX
	HaltDeviceThread(); // Shutdown Devices
	UnmountAllFAT(); // Unmount Devices
	DI_Close(); // Unmount DVD
}

//----------------------------------------------------------------------------
// WIIDS - ExitApp
//----------------------------------------------------------------------------
void ExitApp()
{
	ShutoffRumble(); // Disable Rumble
	SavePrefs(SILENT); // Save Settings
//	if (ROMLoaded && !ConfigRequested && Settings.AutoSave == 1)
//		SaveBatteryOrStateAuto(FILE_FLASH, SILENT); // Save to FLASH
//	else if (ROMLoaded && !ConfigRequested && Settings.AutoSave == 2)
//		SaveBatteryOrStateAuto(FILE_EEPROM, SILENT); // Save to EEPROM
	ExitCleanup();
	if(ShutdownRequested)
		SYS_ResetSystem(SYS_POWEROFF, 0, 0); // Shutdown Wii
	exit(0); // Return to Loader
}

//----------------------------------------------------------------------------
// WIIDS - ShutdownCB
//----------------------------------------------------------------------------
void ShutdownCB()
{
	ShutdownRequested = 1;
}

//----------------------------------------------------------------------------
// WIIDS - ResetCB
//----------------------------------------------------------------------------
void ResetCB()
{
	ResetRequested = 1;
}

//----------------------------------------------------------------------------
// WIIDS - Main
//----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
	__exception_setreload(8);

//	struct armcpu_memory_iface *arm9_memio = &arm9_base_memory_iface; // Duplicate ARM9
//	struct armcpu_ctrl_iface *arm9_ctrl_iface;
//	struct armcpu_memory_iface *arm7_memio = &arm7_base_memory_iface; // Duplicate ARM7
//	struct armcpu_ctrl_iface *arm7_ctrl_iface;
//	cflash_disk_image_file = NULL; // Reset ROM Image

	DI_LoadDVDX(false); // Load DVDX
	DI_Init(); // Initialize Disc Interface
	InitDeviceThread(); // Initialize Devices
	InitializeVideo(); // Initialize Video
	ResetVideo_Menu (); // Switch to Menu Video Mode
	SetupPads(); // Initialize Input
	WPAD_SetPowerButtonCallback((WPADShutdownCallback)ShutdownCB); // Wiimote Power Button
	SYS_SetPowerCallback(ShutdownCB); // System Power Button
	SYS_SetResetCallback(ResetCB); // System Reset Button
	MountAllFAT(); // Initialize libFAT for SD and USB
	InitAudio(); // Initialize Audio
	DefaultSettings (); // Default Settings
	InitFreeType((u8*)font_ttf, font_ttf_size); // Initialize Font System
	InitGUIThreads(); // Initialize GUI
	StartWiiKeyboardMouse(); // Initialize USB Keyboard & Mouse

	if(argc > 0 && argv[0] != NULL)
		CreateAppPath(argv[0]);

//	struct NDS_fw_config_data fw_config; // Build NDS Firmware Settings
//	NDS_FillDefaultFirmwareConfigData(&fw_config); // Fill any un-set with defaults
//  NDS_Init(arm9_memio, &arm9_ctrl_iface, arm7_memio, &arm7_ctrl_iface); // Initialize NDS
//	NDS_CreateDummyFirmware( &fw_config); // Create Dummy NDS Firmware

	while(1) // main loop
	{
		ResumeDeviceThread();
//		if(!ROMLoaded)
			MainMenu(MENU_GAMESELECTION);
//		else
//			MainMenu(MENU_GAME);

		ConfigRequested = 0;
		ScreenshotRequested = 0;

//		enable_sound = true; // Enable NDS Sound
//		if (enable_sound) {
//			SPU_ChangeSoundCore(SNDCORE_SDL, 735 * 4); // Switch to NDS Sound
//		}

		HaltDeviceThread();

		ResetVideo_Emu(); // Switch to NDS Video
//		execute = true; // Switch to NDS Emulation

//		while (NDSEmulation) // Emulation loop
//		{
//			NDSExec(); // NDS Emulation

//			if (enable_sound) {
//				SPU_Emulate_core(); // Emulate ROM Sound
//				SPU_Emulate_user(); // Emulate Mic Sound
//			}
//			if(ResetRequested) {
//				NDS_Reset(); // Reset ROM
//				ResetRequested = 0;
//			}
//			if(ConfigRequested) {
//				ResetVideo_Menu(); // Switch back from NDS Video
//				break;
//			}
//			if(ShutdownRequested)
//				ExitApp();
//		}
	}
	return 0;
}
